<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:pe="http://primefaces.org/ui/extensions"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>#{bundle.village}</title>
        <link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
    </h:head>
    <h:body>
        <ui:composition>
            <h:form >
               <script type="text/javascript">  
                    function handleSaveVillage(xhr, status, args) {  
                        if(args.validationFailed || !args.success) {  
                            jQuery('#dialog').effect("shake", { times:3 }, 100);  
                        } else {  
                            dlgVillage.hide();  
                        }  
                    }

                    function handleDeleteVillage(xhr, status, args) {  
                        if(args.validationFailed || !args.success) {  
                            jQuery('#dialog').effect("shake", { times:3 }, 100);  
                        } else {  
                            dlgDelVillage.hide();  
                        }  
                    }
                    
                    function textOnKeyPress(e) {
                        var code = (e.keyCode ? e.keyCode : e.which);
                        if(code==13) {
                            cmdSearchVillage.jq.click();
                            e.returnValue=false;
                            e.cancel =true;
                            e.preventDefault();
                        }
                    }
                    
                    function componenKeyPress(e) {
                        var code = (e.keyCode ? e.keyCode : e.which);
                        if(code==13) {
                            submitBuslic.jq.click();
                            e.preventDefault();
                        }
                    }

                </script>  

                <p:toolbar>
                    <p:toolbarGroup align="left">
                        <p:commandButton value="#{bundle.btnNew}" icon="ui-icon-document"
                                         action="#{villageBean.newData()}"  
                                         oncomplete="dlgVillage.show()"
                                         process="@this" update=":#{utilityBean.findClientId('displayVillage')}"> 
                            <pe:resetEditableValues for="displayVillage"/>
                        </p:commandButton>

                        <p:commandButton value="#{bundle.btnOpen}" oncomplete="dlgVillage.show()"
                                         actionListener="#{villageBean.openData()}"
                                         icon="ui-icon-disk" 
                                         update=":#{utilityBean.findClientId('displayVillage')}" > 
                        </p:commandButton>

                        <p:commandButton value="#{bundle.btnDelete}" icon="ui-icon-trash" process="@this" onclick="dlgDelVillage.show()" />

                        <p:commandButton type="push" value="#{bundle.btnPrint}" icon="ui-icon-print"/>
                    </p:toolbarGroup>
                    <p:toolbarGroup align="right">
                        <h:panelGrid columns="3" cellpadding="0" cellspacing="0">  
                            <p:inputText id="villageserach" value="#{villageBean.filter}" onkeypress="textOnKeyPress(event)" />  
                            <p:watermark for="villageserach" value="#{bundle.wtmSearch}" />  

                            <p:commandButton widgetVar="cmdSearchVillage" icon="ui-icon-search" update="dtVillages" 
                                             actionListener="#{villageBean.search()}" /> 
                        </h:panelGrid>  
                    </p:toolbarGroup>
                </p:toolbar>
                <br />
                <p:dataTable var="village" value="#{villageBean.villages}" id="dtVillages"
                             selection="#{villageBean.selectedData}" selectionMode="single" 
                             rowKey="#{village.villageId}" resizableColumns="true"
                             rowIndexVar="noUrut" paginator="true" rows="10" paginatorPosition="bottom" 
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                             rowsPerPageTemplate="5,10,15,30">
                     <p:column style="width: 10px">
                    <f:facet name="header">No.</f:facet>
                    <h:outputText value="#{noUrut+1}" />
                 </p:column>
                    <p:column style="width:200px" sortBy="#{village.villageName}">
                        <f:facet name="header">#{bundle.village}</f:facet>
                        <h:outputText value="#{village.villageName}" />
                    </p:column>
                    <p:column style="width:200px" >
                        <f:facet name="header">#{bundle.subdistrict}</f:facet>
                        <h:outputText value="#{village.subdistrict.subdistrictName}" />
                    </p:column>
                </p:dataTable>

                <p:confirmDialog message="#{bundle.confirmDelete}" appendToBody="true" 
                                 header="#{bundle.confirmDeleteHeader}" severity="alert" widgetVar="dlgDelVillage">
                    <h:panelGrid cellspacing="0" cellpadding="0" styleClass="pComBtnD">
                        <f:facet name="footer">
                            <p:commandButton value="#{bundle.btnYes}" update="dtVillages" process="@this"
                                             oncomplete="handleDeleteVillage(xhr, status, args)"  
                                             actionListener="#{villageBean.delete()}" />
                            <p:commandButton value="#{bundle.btnCancel}" onclick="dlgDelVillage.hide()" type="button" /> 
                        </f:facet>
                    </h:panelGrid>   
                </p:confirmDialog>

            </h:form>
            <p:dialog header="#{bundle.village}" widgetVar="dlgVillage" closable="true" dynamic="true" modal="true" appendToBody="true">
                    <p:ajax immediate="true" event="close" listener="#{villageBean.handleClose}"/>                                
                    <h:form>
                    <h:panelGrid id="displayVillage" columns="2" styleClass="addELIT">
                        <h:outputLabel style="padding: 0 15px 0 0" value="#{bundle.village} :" />
                        <h:panelGroup>                            
                            <p:inputText rendered="#{villageBean.insertedData!=null}" value="#{villageBean.insertedData.villageName}" 
                                         required="true" label="#{bundle.globalName}" size="25" />  
                        </h:panelGroup>

                        <h:outputLabel style="padding: 0 15px 0 0" value="#{bundle.subdistrict} :" />
                        <h:panelGroup>                           
                            <p:selectOneMenu value="#{villageBean.insertedData.subdistrict.subdistrictId}" style="width: 170px"
                                             rendered="#{villageBean.insertedData!=null}"
                                             label="#{bundle.subdistrict}" >
                                <f:selectItem itemLabel="#{bundle.Select} #{bundle.subdistrict}" itemValue="" />
                                <f:selectItems value="#{villageBean.avaibleSubdistrict}" />
                            </p:selectOneMenu>
                        </h:panelGroup>
                    </h:panelGrid>

                    <h:panelGrid cellpadding="0" styleClass="pComBtnSTDR">
                        <f:facet name="footer">
                            <p:commandButton value="#{bundle.btnSave}" icon="ui-icon-disk" actionListener="#{villageBean.save()}"
                                             update=":#{utilityBean.findClientId('dtVillages')}"
                                             oncomplete="handleSaveVillage(xhr, status, args)"/>
                            <p:commandButton immediate="true" value="#{bundle.btnClose}" oncomplete="dlgVillage.hide()" icon="ui-icon-document"/>
                        </f:facet>
                    </h:panelGrid>
                        </h:form>
                </p:dialog> 
            
        </ui:composition>
    </h:body>
</html>